<?php
/*************************************************************
* Mambo Community Builder
* Author MamboJoe
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
*************************************************************/
error_reporting(E_ALL ^E_WARNING ^E_NOTICE);

function com_install() {
	$database = &JFactory::getDBO();
	require_once( JPATH_SITE . '/includes/domit/xml_domit_lite_include.php' );
	# Show installation result to user
  ?>
  <center>
	<script language="JavaScript">
	function cbe_ge(objID) {
		return document.getElementById(objID);
	}

	function cbe_toggle(obj) {
		var el = cbe_ge(obj);
		el.style.display = (el.style.display != 'none' ? 'none' : '' );
	}
	</script>
<div id='cbe_installation'>
	<table width=100% style="margin: 10px;" cellpadding=4 cellspacing=4>
		<tr>
		<td width=20%>
		<img src="<?php echo JURI::root() . "components/com_cbe/images/cbelogo-gruen.jpg";?>" width="200" height="200" alt="CBE Logo" title="CBE Logo">
		</td>
		<td width=80% valign=top>
		<h2>Thank you for using CBE!</h2><br />
		For further information, forum and updates please visit <a href='http://www.joomla-cbe.de'>www.joomla-cbe.de</a>.<br />
		<br />
		<a href="index.php?option=com_cbe">Now enter CBE Administration and start...</a>.
		</td>
		</tr>
	</table>
</div>
<div id='cbe_installation_log' style='display:'>
  <table width="100%" border="0">
	<thead>
		<tr>
			<th>Installation Process:</th>
		</tr>
	</thead>
	<tbody>
    <tr>
      <td background="F0F0F0" colspan="1">
        <code>
        <?php

	//+++ Apply db upgrades
	echo "<b>Apply db upgrades:</b><br />";
		
	//sv0.6232
	$database->setQuery("SELECT id FROM #__components WHERE link = 'option=com_cbe' ORDER BY id DESC LIMIT 1");
	$comid = $database->loadresult();

        //Manage Database Upgrades
        $MCBUpgrades = array();

        //Beta 3 Upgrade  ---- done by xml -> without function here
        $MCBUpgrades[0]['test'] = "SELECT `default` FROM #__cbe_lists";
        $MCBUpgrades[0]['updates'][0] = "ALTER TABLE `#__cbe_lists`"
        ."\n ADD `default` TINYINT( 1 ) DEFAULT '0' NOT NULL,"
        ."\n ADD `usergroupids` VARCHAR( 255 ),"
        ."\n ADD `sortfields` VARCHAR( 255 ),"
        ."\n ADD `ordering` INT( 11 ) DEFAULT '0' NOT NULL AFTER `published`";
        $MCBUpgrades[0]['updates'][1] = "UPDATE #__cbe_lists SET `default`=1 WHERE published =1";
        $MCBUpgrades[0]['updates'][2] = "UPDATE #__cbe_lists SET usergroupids = '29, 18, 19, 20, 21, 30, 23, 24, 25', sortfields = '`username` ASC'";
        $MCBUpgrades[0]['updates'][3] = "ALTER TABLE `#__cbe` ADD `acceptedterms` TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER `bannedreason`";
        $MCBUpgrades[0]['message'] = " 1.0 Beta 2 to 1.0 Beta 3.";


        //Beta 4 Upgrade
        $MCBUpgrades[1]['test'] = "SELECT `firstname` FROM #__cbe";
        $MCBUpgrades[1]['updates'][0] = "ALTER TABLE #__cbe ADD `firstname` VARCHAR( 100 ) AFTER `user_id` ,"
        ."\n ADD `middlename` VARCHAR( 100 ) AFTER `firstname` ,"
        ."\n ADD `lastname` VARCHAR( 100 ) AFTER `middlename` ";
        $MCBUpgrades[1]['updates'][1] = "ALTER TABLE `#__cbe_fields` ADD `readonly` TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER `profile`";
        $MCBUpgrades[1]['updates'][3] = "ALTER TABLE `#__cbe_tabs` ADD `width` VARCHAR( 10 ) DEFAULT '.5' NOT NULL AFTER `ordering` ,"
        ."\n ADD `enabled` TINYINT( 1 ) DEFAULT '1' NOT NULL AFTER `width` ,"
        ."\n ADD `plugin` VARCHAR( 255 ) DEFAULT NULL AFTER `enabled`" ;

        $MCBUpgrades[1]['message'] = " 1.0 Beta 3 to 1.0 Beta 4.";

        //CB Enhanced 11-03-2005 to CB Enhanced 14-03-2005
        $MCBUpgrades[2]['test'] = "SELECT `buddy` FROM #__cbe_buddylist";
        $MCBUpgrades[2]['updates'][0] = "ALTER TABLE `#__cbe_buddylist`,"
        ."\n ADD `buddy` tinyint( 4 ) DEFAULT '1' NOT NULL AFTER `buddyid`,"
        ."\n ADD `status` tinyint( 4 ) DEFAULT '0' NOT NULL AFTER `buddy`";
        $MCBUpgrades[2]['updates'][1] = "ALTER TABLE #__cbe_buddylist MODIFY buddy tinyint( 4 ) DEFAULT '0' NOT NULL";
        $MCBUpgrades[2]['message'] = " CB Enhanced 11-03-2005 to CB Enhanced 14-03-2005.";

        //RC 1 Upgrade
        $MCBUpgrades[3]['test'] = "SELECT `fields` FROM #__cbe_tabs";
        $MCBUpgrades[3]['updates'][0] = "ALTER TABLE #__cbe_tabs ADD `plugin_include` VARCHAR( 255 ) AFTER `plugin` ,"
        ."\n ADD `fields` TINYINT( 1 ) DEFAULT '1' NOT NULL AFTER `plugin_include` ";
        $MCBUpgrades[3]['updates'][1] = "INSERT INTO `#__cbe_tabs` (`tabid`, `title`, `description`, `ordering`, `width`, `enabled`, `plugin`, `plugin_include`, `fields`, `sys`) VALUES "
        ."\n (3, '_UE_CONTACT_INFO_HEADER', '', -20, '1', 1, 'getContactTab', NULL, 1, 1)"
        //."\n (4, '_UE_AUTHORTAB', '', -19, '1', 0, 'getAuthorTab', NULL, 0, 1),"
        //."\n (5, '_UE_FORUMTAB', '', -18, '1', 0, 'getForumTab', NULL, 0, 1),"
        //."\n (6, '_UE_BLOGTAB', '', -17, '1', 0, 'getBlogTab', NULL, 0, 1);"
        ;
        $MCBUpgrades[3]['updates'][2] = "ALTER TABLE `#__cbe_lists` ADD `filterfields` VARCHAR( 255 ) AFTER `sortfields`;";
        $MCBUpgrades[3]['message'] = " 1.0 Beta 4 to 1.0 RC 1.";

        //RC 1 to CB Enhanced Upgrade
        $MCBUpgrades[4]['test'] = "SELECT `enhanced_params` FROM #__cbe_tabs";
        $MCBUpgrades[4]['updates'][0] = "ALTER TABLE #__cbe_tabs ADD enhanced_params TINYTEXT DEFAULT NULL AFTER sys";
        $MCBUpgrades[4]['updates'][1] = "INSERT INTO `#__cbe_tabs` (`tabid`, `title`, `description`, `ordering`, `width`, `enabled`, `plugin`, `plugin_include`, `fields`, `sys`, `enhanced_params`) VALUES "
        //."\n ('', '_UE_BUDDY_TAB_LABEL', 		'', '-16', '1', '0', '', NULL, 0, 0, 'profile=1\n tabtype=1\n enhancedname=buddy'),"
        ."\n ('', '_UE_GUESTBOOK_TAB_LABEL', 	'', '-15', '1', '0', '', NULL, 0, 0, 'profile=1\n tabtype=1\n enhancedname=guestbook'),"
        ."\n ('', '_UE_TESTIMONIAL_TAB_LABEL','', '-14', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=testimonials'),"
        //."\n ('', '_UE_SB_FORUM_TAB_LABEL', 		'', '-13', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=sb_forum'),"
        ."\n ('', '_UE_ARTICLES_TAB', 	'', '-12', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=articles'),"
        ."\n ('', '_UE_AUTHOR_TAB_LABEL', 	'', '-11', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=authors'),"
        ."\n ('', '_UE_PROFILE_COMMENTS_TAB_LABEL', 	'', '-10', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=comments'),"
        //."\n ('', '_UE_ZOOM_PHOTOS_TAB_LABEL', 	'', '-9', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=zoom'),"
        //."\n ('', '_UE_SB_MAMBLOG_TAB_LABEL', 		'', '-8', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=mamblog'),"
        ."\n ('', '_UE_MY_PROFILE_JOURNAL_TAB_LABEL', 	'', '-7', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=journal'),"
        ."\n ('', '_UE_LAST_VISITORS_TAB_LABEL', 	'', '-6', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=lastvisitors'),"
        //."\n ('', '_UE_ZOOM_MP3_PHOTOS_TAB_LABEL', 	'', '-5', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=zoom_mp3'),"
        ."\n ('', '_UE_GROUPJIVE_TAB_LABEL', 	'', '-4', '1', '0', '', NULL, 0, 0,'profile=1\n tabtype=1\n enhancedname=groupjive');";
        $MCBUpgrades[4]['updates'][2] = "DELETE FROM #__cbe_tabs WHERE plugin='getAuthorTab';";
        $MCBUpgrades[4]['updates'][3] = "DELETE FROM #__cbe_tabs WHERE plugin='getForumTab';";
        $MCBUpgrades[4]['updates'][4] = "DELETE FROM #__cbe_tabs WHERE plugin='getBlogTab';";
        $MCBUpgrades[4]['message'] = " CB Enhanced and CB RC 1 upgraded to CB Enhanced beta 2.";

	// Upgrades PK Pack
	$MCBUpgrades[5]['test'] = "SELECT `last_ip` FROM #__cbe";
        $MCBUpgrades[5]['updates'][0] = "ALTER TABLE #__cbe ADD last_ip VARCHAR(255) AFTER lastname";
        $MCBUpgrades[5]['message'] = " CBE Beta-1 upgraded for IP-logging.";

	// Zodiac Update Checks
	$MCBUpgrades[7]['test'] = "SELECT `zodiac` FROM #__cbe";
        $MCBUpgrades[7]['updates'][0] = "ALTER TABLE #__cbe ADD zodiac VARCHAR(255) AFTER last_ip";
        $MCBUpgrades[7]['message'] = " CBE Beta-1 upgraded for Zodiac.";
        
	$MCBUpgrades[8]['test'] = "SELECT `zodiac_c` FROM #__cbe";
        $MCBUpgrades[8]['updates'][0] = "ALTER TABLE #__cbe ADD zodiac_c VARCHAR(255) AFTER zodiac";
        $MCBUpgrades[8]['message'] = " CBE Beta-1 upgraded for Zodiac Chinese.";

	// sv0.61 update -> status to guestbook table
	$MCBUpgrades[9]['test'] = "SELECT `status` FROM #__cbe_mambome_gbook";
        $MCBUpgrades[9]['updates'][0] = "ALTER TABLE #__cbe_mambome_gbook ADD status TINYINT(1) DEFAULT '0' NOT NULL AFTER `rating`";
        $MCBUpgrades[9]['updates'][1] = "UPDATE #__cbe_mambome_gbook SET status='1' WHERE status='0'";
        $MCBUpgrades[9]['message'] = " CBE Beta-1 upgraded mambome_guestbook for Status field.";

	// sv0.62 update -> ordering to searchmanager table
	$MCBUpgrades[10]['test'] = "SELECT `ordering` FROM #__cbe_searchmanager";
        $MCBUpgrades[10]['updates'][0] = "ALTER TABLE #__cbe_searchmanager ADD ordering INT(11) DEFAULT '0' AFTER `advanced`";
        $MCBUpgrades[10]['message'] = " CBE Beta-1 upgraded Search-Manager Table for Ordering field.";

	// sv0.621 update -> filter-online to usersList table
	$MCBUpgrades[11]['test'] = "SELECT `filteronline` FROM #__cbe_lists";
        $MCBUpgrades[11]['updates'][0] = "ALTER TABLE #__cbe_lists ADD filteronline TINYINT(1) unsigned NOT NULL DEFAULT '0' AFTER `ordering`";
        $MCBUpgrades[11]['message'] = " CBE Beta-1 upgraded usersList Table for OnlineFilter.";

	// sv0.6232 update
	$MCBUpgrades[12]['test'] = "SELECT `information` FROM #__cbe_fields";
	$MCBUpgrades[12]['updates'][0] = "ALTER TABLE `#__cbe_fields` ADD `information` MEDIUMTEXT  NOT NULL default '' AFTER `type` ";
	$MCBUpgrades[12]['updates'][1] = "ALTER TABLE `#__cbe_fields` ADD `infotag` varchar(10) NOT NULL default 'tag' AFTER `information` ";
	$MCBUpgrades[12]['updates'][2] = "ALTER TABLE `#__cbe_fields` CHANGE `title` `title` VARCHAR( 255 ) NOT NULL";
	$MCBUpgrades[12]['message'] = " CBE Beta-1 upgraded information placeholder for fields.";

	$MCBUpgrades[13]['test'] = "SELECT `aclgroups` FROM #__cbe_tabs";
	$MCBUpgrades[13]['updates'][0] = "ALTER TABLE #__cbe_tabs ADD `aclgroups` varchar(255) NULL AFTER `enhanced_params`";
	$MCBUpgrades[13]['message'] = " CBE Beta-1 upgraded Tabs for ACL-Groups.";
	
	// sv0.6233
	$MCBUpgrades[14]['test'] = "SELECT `aclgroup` FROM #__cbe_lists";
	$MCBUpgrades[14]['updates'][0] = "ALTER TABLE `#__cbe_lists` ADD `aclgroup` INT(9) DEFAULT '-2' NOT NULL AFTER `usergroupids` ";
	$MCBUpgrades[14]['message'] = " CBE Beta-1 upgraded usersLists for ACL-Groups.";

	// sv0.6235
	$MCBUpgrades[15]['test'] = "SELECT `nested` FROM #__cbe_tabs";
	$MCBUpgrades[15]['updates'][0] = "ALTER TABLE #__cbe_tabs ADD `nested` TINYINT(1) UNSIGNED DEFAULT '0' NOT NULL AFTER `aclgroups`";
	$MCBUpgrades[15]['message'] = " CBE Beta-1 upgraded Tabs-DB (nested).";

	$MCBUpgrades[16]['test'] = "SELECT `nest_id` FROM #__cbe_tabs";
	$MCBUpgrades[16]['updates'][0] = "ALTER TABLE #__cbe_tabs ADD `nest_id` INT(11) DEFAULT '-1' NOT NULL AFTER `nested`";
	$MCBUpgrades[16]['message'] = " CBE Beta-1 upgraded Tabs-DB (nest_id).";

	$MCBUpgrades[17]['test'] = "SELECT `is_nest` FROM #__cbe_tabs";
	$MCBUpgrades[17]['updates'][0] = "ALTER TABLE #__cbe_tabs ADD `is_nest` TINYINT(1) UNSIGNED DEFAULT '0' NOT NULL AFTER `nest_id`";
	$MCBUpgrades[17]['message'] = " CBE Beta-1 upgraded Tabs-DB (is_nest).";

	$MCBUpgrades[18]['test'] = "SELECT `q_me` FROM #__cbe_tabs";
	$MCBUpgrades[18]['updates'][0] = "ALTER TABLE #__cbe_tabs ADD `q_me` TEXT DEFAULT NULL AFTER `is_nest`";
	$MCBUpgrades[18]['message'] = " CBE Beta-1 upgraded Tabs-DB (q_me).";

	$MCBUpgrades[19]['test'] = "SELECT `q_you` FROM #__cbe_tabs";
	$MCBUpgrades[19]['updates'][0] = "ALTER TABLE #__cbe_tabs ADD `q_you` TEXT DEFAULT NULL AFTER `q_me`";
	$MCBUpgrades[19]['message'] = " CBE Beta-1 upgraded Tabs-DB (q_you).";

	$MCBUpgrades[20]['test'] = "SELECT `q_bind` FROM #__cbe_tabs";
	$MCBUpgrades[20]['updates'][0] = "ALTER TABLE #__cbe_tabs ADD `q_bind` VARCHAR(3) DEFAULT 'AND' NOT NULL AFTER `q_me`";
	$MCBUpgrades[20]['message'] = " CBE Beta-1 upgraded Tabs-DB (q_bind).";

	// sv0.6236 update -> module to searchmanager table
	$MCBUpgrades[21]['test'] = "SELECT `module` FROM #__cbe_searchmanager";
	$MCBUpgrades[21]['updates'][0] = "ALTER TABLE #__cbe_searchmanager ADD module TINYINT(1) DEFAULT '0' NOT NULL AFTER `ordering`";
	$MCBUpgrades[21]['message'] = " CBE Beta-1 upgraded Search-Manager Table for module field.";

	// sv0.6237 update -> changed acceptedterms and accepteddatasec
	$MCBUpgrades[22]['test'] = "SELECT `accepteddatasec` FROM #__cbe";
	$MCBUpgrades[22]['updates'][0] = "ALTER TABLE #__cbe ADD `last_terms_date` datetime NOT NULL default '0000-00-00 00:00:00' AFTER `acceptedterms`,"
	."\n ADD `accepteddatasec` datetime NOT NULL default '0000-00-00 00:00:00' AFTER `last_terms_date`,"
	."\n ADD `last_datasec_date` datetime NOT NULL default '0000-00-00 00:00:00' AFTER `accepteddatasec`";
	$MCBUpgrades[22]['message'] = " CBE Beta-1 upgraded for Datasecurity-Guideline-accept Feature.";

	// sv0.701 update -> add delete_able column
	$MCBUpgrades[23]['test'] = "SELECT `delete_able` FROM #__cbe_fields";
	$MCBUpgrades[23]['updates'][0] = "ALTER TABLE `#__cbe_fields` ADD `delete_able` TINYINT UNSIGNED DEFAULT '1'";
	$MCBUpgrades[23]['updates'][1] = "UPDATE #__cbe_fields SET delete_able = 0 WHERE sys=1";
	$MCBUpgrades[23]['message'] = " CBE Beta-1 upgraded Fieldlist with 'delete_able' flag.";

	// 1.4.8 update
	$MCBUpgrades[24]['test'] = "SELECT `datetime` FROM #__cbe_buddylist LIMIT 1";
	$MCBUpgrades[24]['updates'][0] = "ALTER TABLE `#__cbe_buddylist` ADD `datetime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP";
	$MCBUpgrades[24]['message'] = " CBE 1.4.8 upgrade buddylist with 'datetime' flag.";
	
	$MCBUpgrades[25]['test'] = "SELECT `statusmsg` FROM #__cbe LIMIT 1";
	$MCBUpgrades[25]['updates'][0] = "ALTER TABLE `#__cbe` ADD `statusmsg` VARCHAR(255) NULL DEFAULT NULL";
	$MCBUpgrades[25]['updates'][1] = "ALTER TABLE `#__cbe` ADD `statusmsg_date` datetime NULL DEFAULT '0000-00-00 00:00:00'";
	$MCBUpgrades[25]['message'] = " CBE 1.4.8 upgrade cbe for statusmsg feature.";
	
	$MCBUpgrades[26]['test'] = "SELECT `tabimg` FROM #__cbe_tabs LIMIT 1";
	$MCBUpgrades[26]['updates'][0] = "ALTER TABLE `#__cbe_tabs` ADD `tabimg` VARCHAR(255) NOT NULL";
	$MCBUpgrades[26]['updates'][1] = "UPDATE #__cbe_tabs SET tabimg='includes/js/ThemeOffice/user.png' WHERE title='_UE_CONTACT_INFO_HEADER'";
	$MCBUpgrades[26]['updates'][2] = "UPDATE #__cbe_tabs SET tabimg='includes/js/ThemeOffice/media.png' WHERE title='GALLERY_TAB_TITLE'";
	$MCBUpgrades[26]['updates'][3] = "UPDATE #__cbe_tabs SET tabimg='includes/js/ThemeOffice/edit.png' WHERE title='_UE_PROFILE_COMMENTS_TAB_LABEL'";
	$MCBUpgrades[26]['updates'][4] = "UPDATE #__cbe_tabs SET tabimg='includes/js/ThemeOffice/document.png' WHERE title='_UE_TESTIMONIAL_TAB_LABEL'";
	$MCBUpgrades[26]['updates'][5] = "UPDATE #__cbe_tabs SET tabimg='includes/js/ThemeOffice/mainmenu.png' WHERE title='_UE_GUESTBOOK_TAB_LABEL'";
	$MCBUpgrades[26]['updates'][6] = "UPDATE #__cbe_tabs SET tabimg='includes/js/ThemeOffice/users.png' WHERE title='_UE_BUDDY_TAB_LABEL'";
	$MCBUpgrades[26]['message'] = " CBE 1.4.8 upgrade tabs with 'tabimg' flag.";
	
	
	//Apply Upgrades
	$i=0;
	foreach ($MCBUpgrades AS $MCBUpgrade)
	{
		$database->setQuery($MCBUpgrade['test']);
		//if it fails test then apply upgrade
		if (!$database->query()) {
			$i++;
			foreach($MCBUpgrade['updates'] as $MCBScript) {
				$database->setQuery($MCBScript);
				if(!$database->query()) {
					//Upgrade failed
					print("<font color=red>".$MCBUpgrade['message']." Failed! SQL error:" . $database->stderr(true)."</font><br />");
					return;
				}
			}
			//Upgrade was successful
			print "<font color=green>".$MCBUpgrade['message']." Upgrade Applied Successfully!</font><br />";
		}
	}
	if ($i==0) print "<font color=blue>Nothing todo...</font><br />";
	
//end..
        ?>

    <!--  </td>
    </tr>
	<tr>
	<td>-->
	<?php
	//+++ Start installing plugins from plugin folder
	//echo "<b>Install plugins:</b><br />";
	//cbe_plugins_install();
	
	//+++ derelvis: delete outdatet db tables
	echo "<b>Remove outdatet db entries:</b><br />";
	
	// 1.4.8 update
	$MCBDels[0]['test'] = "SELECT `tabid` FROM #__cbe_tabs WHERE title = '_UE_ZOOM_PHOTOS_TAB_LABEL' OR enhanced_params LIKE '%enhancedname=zoom%' LIMIT 1";
	$MCBDels[0]['updates'][0] = "DELETE FROM #__cbe_tabs WHERE title = '_UE_ZOOM_PHOTOS_TAB_LABEL'";
	$MCBDels[0]['updates'][1] = "DELETE FROM #__cbe_tabs WHERE enhanced_params LIKE '%enhancedname=zoom%'";
	$MCBDels[0]['message'] = " CBE 1.4.8 delete outdated zoom_tab";
	
	$MCBDels[1]['test'] = "SELECT `tabid` FROM #__cbe_tabs WHERE title = '_UE_ZOOM_MP3_PHOTOS_TAB_LABEL' OR enhanced_params LIKE '%enhancedname=zoom_mp3%' LIMIT 1";
	$MCBDels[1]['updates'][0] = "DELETE FROM #__cbe_tabs WHERE title = '_UE_ZOOM_MP3_PHOTOS_TAB_LABEL'";
	$MCBDels[1]['updates'][1] = "DELETE FROM #__cbe_tabs WHERE enhanced_params LIKE '%enhancedname=zoom_mp3%'";
	$MCBDels[1]['message'] = " CBE 1.4.8 delete outdated zoom_mp3_tab";
	
	$MCBDels[2]['test'] = "SELECT `tabid` FROM #__cbe_tabs WHERE plugin = 'getForumTab' LIMIT 1";
	$MCBDels[2]['updates'][0] = "DELETE FROM #__cbe_tabs WHERE plugin='getForumTab';";
	$MCBDels[2]['message'] = " CBE 1.4.8 delete outdated forum_tab";
	
	$MCBDels[3]['test'] = "SELECT `tabid` FROM #__cbe_tabs WHERE title = '_UE_SB_FORUM_TAB_LABEL' OR title = '_UE_SB_TABTITLE' OR enhanced_params LIKE '%enhancedname=sbforum%' LIMIT 1";
	$MCBDels[3]['updates'][0] = "DELETE FROM #__cbe_tabs WHERE title = '_UE_SB_FORUM_TAB_LABEL' OR title = '_UE_SB_TABTITLE'";
	$MCBDels[3]['updates'][1] = "DELETE FROM #__cbe_tabs WHERE enhanced_params LIKE '%enhancedname=sbforum%'";
	$MCBDels[3]['message'] = " CBE 1.4.8 delete outdated sbforum_tab";

	$MCBDels[4]['test'] = "SELECT `tabid` FROM #__cbe_tabs WHERE title = '_UE_SB_MAMBLOG_TAB_LABEL' OR enhanced_params LIKE '%enhancedname=mamblog%' LIMIT 1";
	$MCBDels[4]['updates'][0] = "DELETE FROM #__cbe_tabs WHERE title = '_UE_SB_MAMBLOG_TAB_LABEL'";
	$MCBDels[4]['updates'][1] = "DELETE FROM #__cbe_tabs WHERE enhanced_params LIKE '%enhancedname=mamblog%'";
	$MCBDels[4]['message'] = " CBE 1.4.8 delete outdated mamblog_tab";	

	//Apply deletions
	$i=0;
	foreach ($MCBDels AS $MCBDel)
	{
		$database->setQuery($MCBDel['test']);
		$database->query();
		$num_rows = $database->getNumRows();
		//if it fails test then apply upgrade
		if ($num_rows>0) {
			$i++;
			foreach($MCBDel['updates'] as $MCBScript) {
				$database->setQuery($MCBScript);
				if(!$database->query()) {
					//Upgrade failed
					print("<font color=red>".$MCBDel['message']." failed! SQL error:" . $database->stderr(true)."</font><br />");
					return;
				}
			}
			//Upgrade was successful
			print "<font color=green>".$MCBDel['message']." successfull!</font><br />";
		}
		//else
			//print "<font color=blue>Nothing todo...</font><br />";
	}
	if ($num_rows==0) print "<font color=blue>Nothing todo...</font><br />";
	

	//+++ derelvis: delete old folders
	echo "<b>Remove outdated folders:</b><br />";
		
	// 1.4.8 Update
	$MCBDeletes[0] = JPATH_SITE . "/components/com_cbe/enhanced/sb_forum";
	$MCBDeletes[1] = JPATH_SITE . "/components/com_cbe/enhanced/zoom";
	$MCBDeletes[2] = JPATH_SITE . "/components/com_cbe/enhanced/zoom_mp3";
	$MCBDeletes[3] = JPATH_SITE . "/components/com_cbe/enhanced/mamblog";
	
	//Apply Deletions
	$i=0;
	foreach ($MCBDeletes AS $MCBDelete)
	{	
		//echo "<br>Processing: ".$MCBDelete;
		if(file_exists($MCBDelete)) {
			$i++;
			//echo "<br>Exists: ".$MCBDelete;
			if (rmdir_rec($MCBDelete)) {
				//Deleted successfully
				print "<font color=green> CBE 1.4.8 delete folder ".$MCBDelete." successfull!</font><br />";
			} else {
				//Delete failed
				print("<font color=red> CBE 1.4.8 delete folder ".$MCBDelete." failed! Please remove manually.</font><br />");
				return;
			}
		}
		//else
			//echo "<br>Not found: ".$MCBDelete;
	}
	if ($i==0) print "<font color=blue>Nothing todo...</font><br />";

	
	//+++ Installation complete, output some information
	$CBE_xml = JPATH_SITE.'/administrator/components/com_cbe/cbe.xml';
	if (file_exists($CBE_xml)) {
		
		$xmlDoc =& new DOMIT_Lite_Document();
		$xmlDoc->resolveErrors( true );
		if (!$xmlDoc->loadXML( $CBE_xml, false, true )) {
			$CBE_version = "error";
		} else {
			$element = &$xmlDoc->documentElement;
			$element = &$xmlDoc->getElementsByPath('version', 1);
			$CBE_version = $element ? $element->getText() : '';
		}
	} else {
		$CBE_version = "CBE successfully";
	}
	?>
		</code>
      </td>
    </tr>
	</tbody>
	<tfoot>
		<tr>
			<td>	
				<font color="green"><b>Installation finished.</b></font><br/>
				<font color="green"><b><?php echo $CBE_version; ?> installed.</b></font>
			</td>
		</tr>
	</tfoot>
  </table>
</div>
  </center>
  <?php
}

function get_cbe_packages($plugins_path) {
	$dir = @opendir($plugins_path);
	$packages = array();
	while( $file = @readdir($dir) ) {
		if( $file != '.' && $file != '..' && is_file($plugins_path . '/' . $file) && !is_link($plugins_path. '/' . $file) ) {
			$packages[] = $file;
		}
	}

	@closedir($dir);

	return $packages;
}

function cbe_plugins_install() {
	error_reporting(E_ALL);
	jimport('joomla.installer.helper');
	jimport('joomla.installer.installer');
	require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_cbe'.DS.'classes'.DS.'CBEPluginInstaller.php');
		
	$package_dir	= JPATH_ADMINISTRATOR.DS.'components'.DS.'com_cbe'.DS.'plugins'.DS;
	$packages	= get_cbe_packages($package_dir);
		
	foreach ($packages as $package) {
		$installerhelper = new JInstallerHelper();
		$tpackage	= $installerhelper->unpack($package_dir.$package);

		// Get an installer instance
		$installer = new JInstaller();//::getInstance();

		$cbe_ext_type = $tpackage['type'];
		$cbeInstaller = new CBEPluginInstaller($installer, $cbe_ext_type);

		/* Fix for a small bug on Joomla on PHP 4 */
		if (version_compare(PHP_VERSION, '5.0.0', '<')) {
			// We use eval to avoid PHP warnings on PHP>=5 versions
			eval("\$installer->setAdapter('".$cbe_ext_type."',&\$cbeInstaller);");
		}else {
			$installer->setAdapter($cbe_ext_type,$cbeInstaller);
		}
		$cbeInstaller->parent = &$installer;
		$install->_adapters[$cbe_ext_type] = &$cbeXinstaller;
		/* End of the fix for PHP <= 4 */

		if (!$installer->install($tpackage['dir']))
			print "<font color=red>Plugin ".basename($tpackage['packagefile'])." couldn't be installed (" . $cbeInstaller->getError() . ")</font><br />";
		else
			print "<font color=green>Plugin ".basename($tpackage['packagefile'])." installed successfully</font><br />";
				
	}
}

//deletes folder recursive
function rmdir_rec($dest)
{
	$list = array_diff(scandir($dest), array('.', '..'));
	foreach ($list as $value) {
		$file = $dest.'/'.$value;
		if (is_dir($file)) { 
			rmdir_rec($file);
		} else { 
			unlink($file);
		}
	}
	return rmdir($dest);
}

?>